package net.caixiaomi.info.ui.football.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.MatchDetailOddsAdapter;
import net.caixiaomi.info.adapter.MatchLineupAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.MatchIntroduceModel;
import net.caixiaomi.info.model.MatchLineUpAlertNateModel;
import net.caixiaomi.info.model.MatchLineupModel;
import net.caixiaomi.info.model.MatchLineupPerson;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by suntian on 2018/7/24.
 */

public class MatchTeamFragment extends BaseFragment implements ComonFragmentImpl {

    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(net.caixiaomi.info.R.id.progress)
    CircularProgressBar mProgress;

    private MatchLineupAdapter mAdapter;
    private MatchLineupModel mData;

    @Override
    protected int fragmentLayout() {
        return R.layout.frag_list;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {

        mProgress.setVisibility(View.GONE);
        mAdapter = new MatchLineupAdapter(null);
        mListView.setAdapter(mAdapter);
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));

    }

    public void setData(MatchLineupModel data) {
        mData=data;
        updateData();
    }

    private void updateData(){
        List<MultiItemEntity> data = new ArrayList<>();
        data.add(mData);

        List<MultiItemEntity> benchData=getBenchData();
        if(benchData.size()==0){
            MatchLineUpAlertNateModel modle=new MatchLineUpAlertNateModel();
            modle.setFirst(true);
            modle.setInjureies(false);
            modle.setNoData(true);
            modle.setHomeTeamAbbr(mData.getHomeTeamAbbr());
            modle.setVisitingTeamAbbr(mData.getVisitingTeamAbbr());
            data.add(modle);
        }
        data.addAll(benchData);

        List<MultiItemEntity> inJureieshData=getInJureieshData();
        if(inJureieshData.size()==0){
            MatchLineUpAlertNateModel modle=new MatchLineUpAlertNateModel();
            modle.setFirst(true);
            modle.setInjureies(true);
            modle.setNoData(true);
            modle.setHomeTeamAbbr(mData.getHomeTeamAbbr());
            modle.setVisitingTeamAbbr(mData.getVisitingTeamAbbr());
            data.add(modle);
        }
        data.addAll(inJureieshData);

        mAdapter.setNewData(data);
    }

    private List<MultiItemEntity> getBenchData(){
        List<MultiItemEntity> list=new ArrayList<>();
        if(mData.getAbenchPersons()==null){
            List<MatchLineupPerson> item=new ArrayList<>();
            mData.setAbenchPersons(item);
        }

        if(mData.getHbenchPersons()==null){
            List<MatchLineupPerson> item=new ArrayList<>();
            mData.setHbenchPersons(item);
        }

        int count=mData.getHbenchPersons().size()>=mData.getAbenchPersons().size()?mData.getHbenchPersons().size():mData.getAbenchPersons().size();

            for(int i=0;i<count;i++){
                MatchLineUpAlertNateModel modle=new MatchLineUpAlertNateModel();
                if(i==0){
                    modle.setFirst(true);
                }else{
                    modle.setFirst(false);
                }
                modle.setInjureies(false);
                if(i<=mData.getAbenchPersons().size()-1){
                    modle.setmVisitorPerson(mData.getAbenchPersons().get(i));
                }
                if(i<=mData.getHbenchPersons().size()-1){
                    modle.setmHomePerson(mData.getHbenchPersons().get(i));
                }
                modle.setHomeTeamAbbr(mData.getHomeTeamAbbr());
                modle.setVisitingTeamAbbr(mData.getVisitingTeamAbbr());
                list.add(modle);
            }
        return list;
    }

    private List<MultiItemEntity> getInJureieshData(){
        List<MultiItemEntity> list=new ArrayList<>();
        if(mData.getAinjureiesPersons()==null){
            List<MatchLineupPerson> item=new ArrayList<>();
            mData.setAinjureiesPersons(item);
        }

        if(mData.getHinjureiesPersons()==null){
            List<MatchLineupPerson> item=new ArrayList<>();
            mData.setHinjureiesPersons(item);
        }

        int count=mData.getHinjureiesPersons().size()>=mData.getAinjureiesPersons().size()?mData.getHinjureiesPersons().size():mData.getAinjureiesPersons().size();

            for(int i=0;i<count;i++){
                MatchLineUpAlertNateModel modle=new MatchLineUpAlertNateModel();
                if(i==0){
                    modle.setFirst(true);
                }else{
                    modle.setFirst(false);
                }
                modle.setInjureies(true);
                if(i<=mData.getHinjureiesPersons().size()-1){
                    modle.setmHomePerson(mData.getHinjureiesPersons().get(i));
                }
                if(i<=mData.getAinjureiesPersons().size()-1){
                    modle.setmVisitorPerson(mData.getAinjureiesPersons().get(i));
                }
                modle.setHomeTeamAbbr(mData.getHomeTeamAbbr());
                modle.setVisitingTeamAbbr(mData.getVisitingTeamAbbr());
                list.add(modle);
            }
        return list;
    }

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }
}
